package com.oopnet.template.test.pool;

import java.util.ArrayList;
import java.util.List;

public class ObjectPool {
	private int maxObjCount=2;
	private int idleObjCount=0;
	private int activeCount=0;
	private List<DataSource> activeDataSources = new ArrayList<DataSource>();
	
	private static ObjectPool objectPool =null;
	
	static class SingletonClass{
		//静态变量在类加载时初始化，且只初始化一次。
		private static ObjectPool objectPool = new ObjectPool();
	}
	
	public static ObjectPool getPoolInstance(){
		return SingletonClass.objectPool;
	}
	
	public DataSource popInstance(){
		synchronized (this) {
			if(this.activeCount<this.maxObjCount){
				DataSource dataSource = new DataSource();
				this.activeDataSources.add(dataSource);
				this.activeCount++;
				return dataSource;
			}else{
				return this.activeDataSources.get(0);
			}
		}
			
	}
	
	
	
	public static void main(String[] args) {
		ObjectPool pool1 = ObjectPool.getPoolInstance();
		DataSource dataSource1 = pool1.popInstance();
		DataSource dataSource2 = pool1.popInstance();
		DataSource dataSource3 = pool1.popInstance();
		System.out.println(dataSource1==dataSource3);
	}
	
	private ObjectPool() {
		
	}
	
	
	public int getMaxObjCount() {
		return maxObjCount;
	}
	public void setMaxObjCount(int maxObjCount) {
		this.maxObjCount = maxObjCount;
	}
	public int getIdleObjCount() {
		return idleObjCount;
	}
	public void setIdleObjCount(int idleObjCount) {
		this.idleObjCount = idleObjCount;
	}
	public int getActiveCount() {
		return activeCount;
	}
	public void setActiveCount(int activeCount) {
		this.activeCount = activeCount;
	}
	
}
